<template>
  <div class="register pageFlexColumn pageAlignEnd">
    <div class="registerContent pageFlexColumn pageFlexCenter">
      <div class="pageFlex pageAlignCenter" style="width: 61%;">
        <img src="@/assets/logo/logo_fang.png" alt="易创燃气" class="logo">
        <div class="title">易创燃气智慧管理平台</div>
      </div>
      <div class="registerText">欢迎注册</div>
      <el-form class="formBox" ref="registerRef" :model="registerForm" :rules="registerRules">
        <el-form-item class="partBox bgFF" prop="username">
          <el-input class="input" type="text" size="large" auto-complete="off" placeholder="请输入账号" v-model="registerForm.username">
            <template #prefix>
              <img src="../assets/images/login_user.png" alt="账号" class="icon">
            </template>
          </el-input>
        </el-form-item>
        <el-form-item class="partBox bgFF" prop="password">
          <el-input class="input" :type="showPwd1 ? 'text' : 'password'" size="large" auto-complete="off" placeholder="请输入密码" 
            v-model="registerForm.password" @keyup.enter="handleRegister">
            <template #prefix>
              <img src="../assets/images/login_lock.png" alt="密码" class="icon">
            </template>
            <template #suffix>
              <div class="pwdIcon" :class="{'active': showPwd1}" @click="showPwd1 = !showPwd1">
                <img src="../assets/images/login_pwdShow.png" alt="密码" class="icon icon1"/>
                <img src="../assets/images/login_pwd.png" alt="密码" class="icon icon2"/>
              </div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item class="partBox bgFF" prop="confirmPassword">
          <el-input class="input" :type="showPwd2 ? 'text' : 'password'" size="large" auto-complete="off" placeholder="请再次输入确认密码" 
            v-model="registerForm.confirmPassword" @keyup.enter="handleRegister">
            <template #prefix>
              <img src="../assets/images/login_lock.png" alt="密码" class="icon">
            </template>
            <template #suffix>
              <div class="pwdIcon" :class="{'active': showPwd2}" @click="showPwd2 = !showPwd2">
                <img src="../assets/images/login_pwdShow.png" alt="密码" class="icon icon1"/>
                <img src="../assets/images/login_pwd.png" alt="密码" class="icon icon2"/>
              </div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item class="partBox bgFF pageFlex" prop="code" v-if="captchaEnabled">
          <el-input class="input" size="large" auto-complete="off" placeholder="请输入验证码"
            v-model="registerForm.code" @keyup.enter="handleRegister">
            <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon"/></template>
          </el-input>
          <div class="loginCode pageFlex pageFlexCenter">
            <img class="img" :src="codeUrl" @click="getCode"/>
          </div>
        </el-form-item>
        <div class="pageFlex pageAlignCenter pageSpaceEnd" style="width: 100%;margin-top: 18px;">
          <router-link class="forgetPwd" :to="'/login'">使用已有账户登录</router-link>
        </div>
        <el-form-item style="width: 100%;margin-top: 41px;">
          <el-button size="large" type="primary" class="btnBox"
            :loading="loading" @click.prevent="handleRegister">
            <span class="btnText">{{loading ? '注 册 中...' : '注 册'}}</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ElMessageBox } from "element-plus";
import { getCodeImg, register } from "@/api/login";

const router = useRouter();
const { proxy } = getCurrentInstance();

const registerForm = ref({
  username: "",
  password: "",
  confirmPassword: "",
  code: "",
  uuid: ""
});

const equalToPassword = (rule, value, callback) => {
  if (registerForm.value.password !== value) {
    callback(new Error("两次输入的密码不一致"));
  } else {
    callback();
  }
};

const registerRules = {
  username: [
    { required: true, trigger: "blur", message: "请输入您的账号" },
    { min: 2, max: 20, message: "用户账号长度必须介于 2 和 20 之间", trigger: "blur" }
  ],
  password: [
    { required: true, trigger: "blur", message: "请输入您的密码" },
    { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" },
    { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符：< > \" ' \\\ |", trigger: "blur" }
  ],
  confirmPassword: [
    { required: true, trigger: "blur", message: "请再次输入您的密码" },
    { required: true, validator: equalToPassword, trigger: "blur" }
  ],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }]
};

const showPwd1 = ref(false);
const showPwd2 = ref(false);
const codeUrl = ref("");
const loading = ref(false);
const captchaEnabled = ref(true);

function handleRegister() {
  proxy.$refs.registerRef.validate(valid => {
    if (valid) {
      loading.value = true;
      register(registerForm.value).then(res => {
        const username = registerForm.value.username;
        ElMessageBox.alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", "系统提示", {
          dangerouslyUseHTMLString: true,
          type: "success",
        }).then(() => {
          router.push("/login");
        }).catch(() => {});
      }).catch(() => {
        loading.value = false;
        if (captchaEnabled) {
          getCode();
        }
      });
    }
  });
}

function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img;
      registerForm.value.uuid = res.uuid;
    }
  });
}

getCode();
</script>
<style lang='scss' scoped>
@import "@/assets/styles/public.scss";
.register{
  width: 100%;
  height: 100vh;
  background-image: url('../assets/images/login_bg.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  overflow: hidden;
  .registerContent{
    width: 50%;
    height: 100%;
    .logo{
      width: 56px;
      height: 56px;
      display: block;
    }
    .title{
      margin-left: 14px;
      height: 48px;
      font-weight: 500;
      font-size: 24px;
      color: #0A1B39;
      line-height: 48px;
      text-shadow: 0px 4px 4px rgba(190,190,190,0.46);
    }
    .registerText{
      margin-top: 20px;
      width: 61%;
      height: 44px;
      font-weight: 400;
      font-size: 35px;
      color: #0A1B39;
      line-height: 44px;
    }
    .formBox{
      width: 61%;
      margin-top: 16px;
      .partBox{
        width: 100%;
        height: 44px;
        border-radius: 6px;
        margin: 24px 0 0 0;
        flex-wrap: nowrap;
        .input{
          width: 100%;
          height: 100%;
          flex: 1;
          &:deep(.el-input__wrapper){
            box-shadow: none;
          }
          .icon{
            width: 15px;
            height: 15px;
          }
          .pwdIcon{
            cursor: pointer;
            .icon1{
              display: none;
            }
            .icon2{
              display: block;
            }
            &.active{
              .icon1{
                display: block;
              }
              .icon2{
                display: none;
              }
            }
          }
        }
        .loginCode{
          width: auto;
          height: 100%;
          cursor: pointer;
          overflow: hidden;
          .img{
            width: 100%;
            height: 100%;
          }
        }
      }
      .forgetPwd{
        height: 20px;
        font-weight: 400;
        font-size: 14px;
        color: #165DFF;
        line-height: 20px;
        cursor: pointer;
        user-select: none;
      }
      .btnBox{
        width: 100%;
        height: 53px;
        background: #165DFF;
        border-radius: 6px;
        .btnText{
          font-weight: 500;
          font-size: 20px;
          color: #FFFFFF;
        }
      }
    }
  }
}
</style>
